package com.chenlonghai.sbmybatis.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.chenlonghai.sbmybatis.entity.Banner;
import com.chenlonghai.sbmybatis.entity.Cases;
import com.chenlonghai.sbmybatis.entity.ResultModel;
import com.chenlonghai.sbmybatis.service.BannerService;


@RestController
@RequestMapping("/banner")
public class BnanerController {
	@Value("${cbs.location}")
    private String locationPath;
	@Autowired
	private BannerService bannerService;
	
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public Map<String, Object> listBanner(String type){
		
		System.out.println(type);
		Map<String, Object>modelMap = new HashMap<>();
		List<Banner>list = bannerService.queryBannerByType(type);
		ResultModel<List>result = new ResultModel<List>(list,modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}
	
	
	@RequestMapping(value="/abanner",method = RequestMethod.GET)
	public Map<String, Object> listBanner(Integer id){
		
		System.out.println(id);
		Map<String, Object>modelMap = new HashMap<>();
		Banner banner = bannerService.queryBannerById(id);
		ResultModel<Banner>result = new ResultModel<Banner>(banner,modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}
	

	@RequestMapping(value="/deleteById",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public Map<String, Object> deleteById(Integer id){

		Map<String, Object>modelMap = new HashMap<>();
		boolean isOk  = bannerService.deleteBannerById(id);
		if (isOk) {
			ResultModel<String>result = new ResultModel<String>("",modelMap);
			modelMap = result.getResltMap();
			return modelMap;	
		}else{
			ResultModel<String>result = new ResultModel<String>(null,modelMap);
			modelMap = result.getResltMap();
			return modelMap;	
		}

	}
	
	@RequestMapping(value="/alllist",method = RequestMethod.GET)
	public Map<String, Object> alllistBanner(){
		
	
		Map<String, Object>modelMap = new HashMap<>();
		List<Banner>list = bannerService.queryBanner();
		ResultModel<List>result = new ResultModel<List>(list,modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}
	
	@RequestMapping(value="/add",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public Map<String, Object> addBanner(
            HttpServletRequest request,Banner banner){
		

		
		
		Map<String, Object>modelMap = new HashMap<>();
		 boolean isOk = false;
        System.out.println(banner.getImgUrl());
        System.out.println(banner.getLinkUrl());
        System.out.println(banner.getType());
        banner.setTittle("暂无");
        banner.setCreateTime(new Date());
        banner.setUpdateTime(new Date());
        isOk =   bannerService.addBannner(banner);
        
        if (isOk) {
        	ResultModel<String>result = new ResultModel<String>("",modelMap);
        	 return  result.getResltMap();
		}
     	ResultModel<String>result = new ResultModel<String>(null,modelMap);
     	
        return result.getResltMap();


	}
	
	@RequestMapping(value="/update",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public Map<String, Object> updateBanner(
            HttpServletRequest request,Banner banner){

		Map<String, Object>modelMap = new HashMap<>();
		
		System.out.println("测试更新");
		 boolean isOk = false;
        System.out.println(banner.getImgUrl());
        System.out.println(banner.getLinkUrl());
        System.out.println(banner.getType());
        banner.setTittle("暂无");
        banner.setUpdateTime(new Date());
        isOk =   bannerService.updateBanner(banner);
        
        if (isOk) {
        	ResultModel<String>result = new ResultModel<String>("",modelMap);
        	 return  result.getResltMap();
		}
     	ResultModel<String>result = new ResultModel<String>(null,modelMap);
     	
        return result.getResltMap();


	}
	
}
